package test;

import java.text.DateFormat;
import java.util.Date;
import java.util.LinkedHashMap;

import org.jfree.data.time.Day;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RefineryUtilities;

import exception.ViewException;

import view.jfreechartTest.TimeSeriesExample;
import view.jfreechartTest.XYStepRendererDemo1;

public class DrawBalance {

	private String[] getDate(String s) {

		String[] ss = new String[40];

		for (int i = 0; i < ss.length; i++) {

			ss = s.split("-");


		}

		return ss;
	}

	public static void drawStepChart(String chartName, TimeSeriesCollection dataset)
	{
		XYStepRendererDemo1 xysteprendererdemo1 = new XYStepRendererDemo1(chartName, dataset);
        xysteprendererdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(xysteprendererdemo1);
        xysteprendererdemo1.setVisible(true);
	}
	
	public static void drawSingleBalance(String tableName, String cardId) throws ViewException{
		//单卡消费图，建view
		//TODO: 修改计算和读取数据
		//TODO: 问题：并不是画的余额，而是画的每日充值/消费总和。anyway,先不管了 
		DrawBalance db =new DrawBalance();

		CalculateBalance cb = new CalculateBalance();

		String merge_pay = "merge_totalpay_id";

		String merge_recharge = "merge_totalrecharge_id";
		

		int weight_minus = -1;
		int weight_zero = 0;
		int weight_one = 1;

		//TimeSeriesExample tse = new TimeSeriesExample();

		TimeSeriesCollection dataset = new TimeSeriesCollection();

		String legendName = "余额变化";

		@SuppressWarnings("deprecation")
		TimeSeries pop = new TimeSeries(legendName, Second.class);

		LinkedHashMap<Date, Double> payMap = new LinkedHashMap<Date, Double>();

		LinkedHashMap<Date, Double> rechargeMap = new LinkedHashMap<Date, Double>();

		LinkedHashMap<Date, Double> resultMap = new LinkedHashMap<Date, Double>();

		payMap = cb.makeHashMap(tableName,"pay",cardId, payMap);

		rechargeMap = cb.makeHashMap(tableName, "recharge", cardId, rechargeMap);

		resultMap = cb.mergeHashMaps(rechargeMap, payMap,weight_one,
				weight_minus);
		
		
//		resultMap = cb.mergeHashMaps(resultMap, pospayMap,weight_zero,
//				weight_minus);
//		
//		resultMap = pospayMap;

//		double former = 0;
		for (Date date : resultMap.keySet()) {
			String s = DateFormat.getDateInstance().format(date);

			String[] ss = db.getDate(s);
			
			pop.addOrUpdate(new Day(Integer.parseInt(ss[2]), Integer.parseInt(ss[1]), Integer.parseInt(ss[0])), 
					resultMap.get(date));	
//			former += resultMap.get(date);
			
		}
		dataset.addSeries(pop);
		
		//画step图
		//tse.DrawXYChart(dataset, chartName);

        drawStepChart("单卡余额变化图",dataset);
	}
	
	//TODO: totalBalance
	public static TimeSeriesCollection drawTotalBalance(String tableName, String legendName) throws ViewException{
		DrawBalance db =new DrawBalance();

		CalculateBalance cb = new CalculateBalance();

		String merge_onlinepay = "merge_onlinepay";

		String merge_recharge = "merge_onlinerecharge";
		
		String merge_pospay = "merge_pospay";

		int weight_minus = -1;
		int weight_zero = 0;
		int weight_one = 1;

		//TimeSeriesExample tse = new TimeSeriesExample();

		TimeSeriesCollection dataset = new TimeSeriesCollection();

		//String legendName = "余额变化";

		@SuppressWarnings("deprecation")
		TimeSeries pop = new TimeSeries(legendName, Second.class);

		LinkedHashMap<Date, Double> payMap = new LinkedHashMap<Date, Double>();

		LinkedHashMap<Date, Double> rechargeMap = new LinkedHashMap<Date, Double>();
		
		//LinkedHashMap<Date, Double> pospayMap = new LinkedHashMap<Date, Double>();

		LinkedHashMap<Date, Double> resultMap = new LinkedHashMap<Date, Double>();

		payMap = cb.makeHashMap(tableName,"pay", "0", payMap);

		rechargeMap = cb.makeHashMap(tableName, "recharge", "0", rechargeMap);
		
		//pospayMap = cb.makeHashMap(merge_pospay, "0", pospayMap);

		resultMap = cb.mergeHashMaps(rechargeMap, payMap,weight_one,
				weight_minus);
		
		
		//resultMap = cb.mergeHashMaps(resultMap, pospayMap,weight_zero,weight_minus);
		
//		resultMap = pospayMap;

//		double former = 0;
		for (Date date : resultMap.keySet()) {
			String s = DateFormat.getDateInstance().format(date);

			String[] ss = db.getDate(s);
			
			
			pop.addOrUpdate(new Day(Integer.parseInt(ss[2]), Integer.parseInt(ss[1]), Integer.parseInt(ss[0])), 
					resultMap.get(date));	
//			former += resultMap.get(date);
			
		}
		dataset.addSeries(pop);
		return dataset;
		
		//画step图
		//tse.DrawXYChart(dataset, chartName);

        //drawStepChart("整体余额变化图",dataset);
	}
	
	public static void main(String args[]) {
		
	/*	try {
			TimeSeriesCollection dataset = DrawBalance.drawTotalBalance("整体余额");
			drawStepChart("整体余额变化图",dataset);
		} catch (ViewException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */
		//DrawBalance.drawSingleBalance("5088010100000011235");
		

		
		
//插值，精确反映拐点
		
//		LinkedHashMap<Date,Double> modifiedMap = new LinkedHashMap<Date,Double>();
//		modifiedMap = cb.makeInsertedHashMap(resultMap);
//		
//		TimeSeriesExample tse2 = new TimeSeriesExample();
//
//		TimeSeriesCollection dataset2 = new TimeSeriesCollection();
//
//		String chartName2 = "余额变化";
//		
//		TimeSeries pop2 = new TimeSeries(chartName2, Second.class);
//		
//		for (Date date : modifiedMap.keySet()) {
//			String s = DateFormat.getDateInstance().format(date);
//		
//
//			String[] ss = db.getDate(s);
//			
//			pop2.addOrUpdate(new Day(Integer.parseInt(ss[2]), Integer.parseInt(ss[1]), Integer.parseInt(ss[0])), 
//					modifiedMap.get(date));	
//			
//		}
//		dataset2.addSeries(pop2);
//		
//		tse2.DrawXYChart(dataset2, chartName2);
		
		
	}

}
